layui.define(['tool'], function (exports) {
	const layer = layui.layer, tool = layui.tool,form=layui.form, table=layui.table, upload = layui.upload;
	// 查找指定的元素在数组中的位置
	Array.prototype.indexOf = function (val) {
		for (var i = 0; i < this.length; i++) {
			if (this[i] == val) {
				return i;
			}
		}
		return -1;
	};
	// 通过索引删除数组元素
	Array.prototype.remove = function (val) {
		var index = this.indexOf(val);
		if (index > -1) {
			this.splice(index, 1);
		}
	};
	
	const obj = {
		addFile: function (options) {
			let that = this;
			let settings = {
				type:0,
				btn: 'uploadBtn',
				box: 'fileBox',
				url: "/admin/api/upload",
				accept: 'file', //普通文件
				exts: 'png|jpg|gif|jpeg|doc|docx|ppt|pptx|xls|xlsx|pdf|zip|rar|7z', //只允许上传文件格式
				colmd:4,
				isSave:false,
				ajaxSave:function(val){},
				ajaxDelete:function(val){}
			};
			if(options.qn_url!=undefined){
				settings.url = options.qn_url;
			}
			let opts = $.extend({}, settings, options);
			if(opts.type==0){
				//虚拟删除
				$('#'+opts.box).on('click', '.btn-delete', function () {

					let file_id = $(this).data('id');
					layer.confirm('确定删除该附件吗？', {
						icon: 3,
						title: '提示'
					}, function(index) {
						let idsStr = $('#'+opts.box+'Input').val(),idsArray = [];
						if (idsStr != '') {
							idsArray = idsStr.split(",");
						}
						idsArray.remove(file_id);
						$('#'+opts.box+'Input').val(idsArray.join(','));
						$('#fileItem' + file_id).remove();
						layer.close(index);
					});
				})
				
				//ajax删除
				$('#'+opts.box).on('click', '.ajax-delete', function () {
					let file_id = $(this).data('id');
					let idsStr = $('#'+opts.box+'Input').val(),idsArray = [];
					if (idsStr != '') {
						idsArray = idsStr.split(",");
					}
					idsArray.remove(file_id);
					layer.confirm('确定删除该附件吗？', {
						icon: 3,
						title: '提示'
					}, function(index) {
						if (typeof (opts.ajaxSave) == "function") {
							opts.ajaxSave(idsArray.join(','));
						}
						layer.close(index);
					});
				})
			}
			if(opts.type==1){
				//ajax删除
				$('#'+opts.box).on('click', '.ajax-delete', function () {
					let file_id = $(this).data('id');
					layer.confirm('确定删除该附件吗？', {
						icon: 3,
						title: '提示'
					}, function(index) {
						if (typeof (opts.ajaxSave) == "function") {
							opts.ajaxDelete(file_id);
						}
						layer.close(index);
					});
				})
			}
			
			//多附件上传
			upload.render({
				elem: '#'+opts.btn,
				url: opts.url,
				accept: opts.accept,
				exts: opts.exts,
				multiple: true,
				done: function(res){
					layer.msg(res.msg);
					if (res.code == 0) {
						//上传成功
						if(opts.type==0){
							let idsStr = $('#'+opts.box+'Input').val(),idsArray = [];
							if (idsStr != '') {
								idsArray = idsStr.split(",");
							}
							idsArray.push(res.data.id);
							$('#'+opts.box+'Input').val(idsArray.join(','));
							let imageicon = '<i class="file-icon iconfont icon-renwuguanli"></i>';
							if(res.data.fileext=='jpg'||res.data.fileext=='png'||res.data.fileext=='jpeg'){
								imageicon = `<img src="${res.data.filepath}" alt="" style="width: 50px;height: 50px;" class="file-img">`
							}
							if(res.data.fileext=='mp4'||res.data.fileext=='avi'){
								imageicon = `<a href="${res.data.external_links}" target="_blank">查看</a>&nbsp`
							}
							let temp = `<div class="layui-col-md${opts.colmd}" id="fileItem${res.data.id}">
									<div class="file-card">
											${imageicon}
										<div class="file-title">${res.data.name}</div>
										<div class="file-tool">
											<a href="${res.data.filepath}" download="${res.data.name}" style="font-size: 14px;" title="下载查看" target="_blank">下载</a>
											<span class="btn-delete iconfont icon-shanchu red" style="font-size: 14px;" data-id="${res.data.id}" title="删除">删除</span>
										</div>
									</div>
								</div>`;
							$('#'+opts.box).append(temp);
							
							if (typeof (opts.ajaxSave) == "function" && opts.isSave ==true) {
								opts.ajaxSave(idsArray.join(','));
							}
						}
						if(opts.type==1){
							if (typeof (opts.ajaxSave) == "function" && opts.isSave ==true) {
								opts.ajaxSave(res);
							}
						}
					}
				}
			});
		}
	};

	
	exports('oaTool', obj);
});  